package com.example.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.example.mybatis.domain.ShopExt;
import com.example.mybatis.mapper.ShopExtMapper;
import com.example.service.IShopExtService;
import com.example.vo.AmapResult;
import com.example.vo.Geocode;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static java.util.stream.Collectors.toList;

/**
 * 商店详细Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-06-18
 */
@Service
@Slf4j
public class ShopExtServiceImpl implements IShopExtService
{
    @Autowired
    private ShopExtMapper shopExtMapper;
    @Autowired
    @Qualifier("threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor executor;

    @Override
    public void getShopGeoBatch() {

        PageHelper.startPage(0, 5000, "id");
        ShopExt para = new ShopExt();
        para.setStatus("0");
        List<ShopExt> shops = shopExtMapper.selectShopExtList(para);

        List<CompletableFuture<ShopExt>> res = shops.stream()
                .map(s -> CompletableFuture.supplyAsync(() -> invokeAmapApi(s), executor))
                .collect(toList());

        List<ShopExt> shopList = res.stream()
                .map(CompletableFuture::join)
                .filter(s -> "1".equals(s.getStatus())).collect(toList());

        if (CollectionUtil.isNotEmpty(shopList)) {
            shopExtMapper.updateShopExtBatch(shopList);
        }
    }

    private ShopExt invokeAmapApi(ShopExt shop) {

        String url = "https://restapi.amap.com/v3/geocode/geo";
        Map<String, Object> param = new HashMap<>();
        param.put("key", "3380c71d844f388d74639ae48b1289c7");
//        param.put("key", "b337cf55d5d280581ed42ff406f997c1");
        param.put("address", shop.getAddress());

        String res = HttpUtil.get(url, param);
        AmapResult result = JSON.parseObject(res, AmapResult.class);

        if (result.getCount() > 0) {
            Geocode geocode = result.getGeocodes().get(0);
            shop.setStatus("1");
            shop.setAdcode(geocode.getAdcode());
            shop.setLocation(geocode.getLocation());
            shop.setUpdateTime(new Date());
        }

        return shop;
    }


    /**
     * 查询商店详细
     * 
     * @param id 商店详细ID
     * @return 商店详细
     */
    @Override
    public ShopExt selectShopExtById(Long id)
    {
        return shopExtMapper.selectShopExtById(id);
    }

    /**
     * 查询商店详细列表
     * 
     * @param shopExt 商店详细
     * @return 商店详细
     */
    @Override
    public List<ShopExt> selectShopExtList(ShopExt shopExt)
    {
        return shopExtMapper.selectShopExtList(shopExt);
    }

    /**
     * 新增商店详细
     * 
     * @param shopExt 商店详细
     * @return 结果
     */
    @Override
    public int insertShopExt(ShopExt shopExt)
    {
        return shopExtMapper.insertShopExt(shopExt);
    }

    /**
     * 修改商店详细
     * 
     * @param shopExt 商店详细
     * @return 结果
     */
    @Override
    public int updateShopExt(ShopExt shopExt)
    {
        return shopExtMapper.updateShopExt(shopExt);
    }

    /**
     * 删除商店详细对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteShopExtByIds(String ids)
    {
//        return shopExtMapper.deleteShopExtByIds(Convert.toStrArray(ids));
        return 0;
    }

    /**
     * 删除商店详细信息
     * 
     * @param id 商店详细ID
     * @return 结果
     */
    @Override
    public int deleteShopExtById(Long id)
    {
        return shopExtMapper.deleteShopExtById(id);
    }
}
